﻿using Omabar.Model;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Tom.Net;

namespace Omabar.Entity
{
    public class Trip_Hotel_Entity : EntityBase<TRIP_HOTEL>
    {
        public override RESP_Info Add(TRIP_HOTEL model)
        {
            RESP_Info info = new RESP_Info();
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "insert into TRIP_HOTEL(TRIP_ID,HOTEL_ID) values (@TRIP_ID,@HOTEL_ID)";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("TRIP_ID", model.TRIP_ID));
                    cmd.Parameters.Add(new SqlParameter("HOTEL_ID", model.HOTEL_ID));
                    cmd.ExecuteNonQuery();

                    info.Success = true;
                    info.Title = "成功";
                    info.Tip = "<h5><p>添加行程酒店成功</p></h5>";
                    info.Url = "TripHotelList.aspx";
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    info.Success = true;
                    info.Title = "错误";
                    info.Tip = "<h5><p>添加行程酒店失败</p></h5>";
                    info.Url = "TripHotelAdd.aspx";

                    Tom.Net.Log.LogManager.Error("添加行程酒店失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return info;
        }

        public override RESP_Info Update(TRIP_HOTEL model)
        {
            RESP_Info info = new RESP_Info();
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "update TRIP_HOTEL set HOTEL_ID=@HOTEL_ID where TRIP_ID=@TRIP_ID";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("TRIP_ID", model.TRIP_ID));
                    cmd.Parameters.Add(new SqlParameter("HOTEL_ID", model.HOTEL_ID));
                    cmd.ExecuteNonQuery();

                    info.Success = true;
                    info.Title = "编辑";
                    info.Tip = "<h5><p>编辑行程酒店成功</p></h5>";
                    info.Url = "TripHotelList.aspx";
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    info.Success = true;
                    info.Title = "错误";
                    info.Tip = "<h5><p>编辑行程酒店失败</p></h5>";
                    info.Url = "TripHotelAdd.aspx";

                    Tom.Net.Log.LogManager.Error("编辑行程酒店失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return info;
        }

        public override RESP_Info Delete(TRIP_HOTEL tripHotel)
        {
            RESP_Info info = new RESP_Info();
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "delete from TRIP_HOTEL where TRIP_ID=@TRIP_ID AND HOTEL_ID=@HOTEL_ID";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("TRIP_ID", tripHotel.TRIP_ID));
                    cmd.Parameters.Add(new SqlParameter("HOTEL_ID", tripHotel.HOTEL_ID));

                    cmd.ExecuteNonQuery();

                    info.Success = true;
                    info.Title = "删除";
                    info.Tip = "<h5><p>删除行程酒店成功</p></h5>";
                    info.Url = "TripHotelList.aspx";
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    info.Success = true;
                    info.Title = "错误";
                    info.Tip = "<h5><p>删除行程酒店失败</p></h5>";
                    info.Url = "TripHotelList.aspx";

                    Tom.Net.Log.LogManager.Error("删除行程酒店失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return info;
        }

        public override TRIP_HOTEL GetModel(string id)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "select TRIP_ID,HOTEL_ID from TRIP_HOTEL where TRIP_ID=@TRIP_ID";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("TRIP_ID", id));

                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        TRIP_HOTEL tripHotel = new TRIP_HOTEL();
                        if (!reader.IsDBNull(0)) tripHotel.TRIP_ID = reader.GetString(0);
                        if (!reader.IsDBNull(1)) tripHotel.HOTEL_ID = reader.GetString(1);
                        reader.Close();
                        reader.Dispose();
                        cmd.Dispose();
                        return tripHotel;
                    }
                }
                catch (Exception ex)
                {
                    Tom.Net.Log.LogManager.Error("获取行程酒店数据失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }
            return null;
        }

        public override TRIP_HOTEL GetModelBySQL(string sql)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = sql;
                    cmd.CommandType = System.Data.CommandType.Text;

                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        TRIP_HOTEL tripHotel = new TRIP_HOTEL();
                        if (!reader.IsDBNull(0)) tripHotel.TRIP_ID = reader.GetString(0);
                        if (!reader.IsDBNull(1)) tripHotel.HOTEL_ID = reader.GetString(1);

                        reader.Close();
                        reader.Dispose();
                        cmd.Dispose();
                        return tripHotel;
                    }
                }
                catch (Exception ex)
                {
                    Tom.Net.Log.LogManager.Error("获取行程酒店数据失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }
            return null;
        }

        public override List<TRIP_HOTEL> GetModels(string sql)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = sql;
                    cmd.CommandType = System.Data.CommandType.Text;

                    SqlDataReader reader = cmd.ExecuteReader();
                    List<TRIP_HOTEL> tripHotels = new List<TRIP_HOTEL>();
                    while (reader.Read())
                    {
                        TRIP_HOTEL tripHotel = new TRIP_HOTEL();
                        if (!reader.IsDBNull(0)) tripHotel.TRIP_ID = reader.GetString(0);
                        if (!reader.IsDBNull(1)) tripHotel.HOTEL_ID = reader.GetString(1);

                        tripHotels.Add(tripHotel);
                    }
                    reader.Close();
                    reader.Dispose();
                    cmd.Dispose();

                    return tripHotels;
                }
                catch (Exception ex)
                {
                    Tom.Net.Log.LogManager.Error("获取行程酒店数据失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return null;
        }
    }
}

